using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;
using System.IO;

public partial class Admin_SanPhamCT : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
            LoadDataHinh();
        }       
    }
    private void LoadDataHinh()
    {
        int id = int.Parse(Request.QueryString["id"].ToString());
        Criteria cri = new Criteria();
        int total = 0;
        cri.add(Condition.AND, tbHinhAnh.tbHinhAnhColumns.IDSanPham, Condition.EQUAL, int.Parse(id.ToString()));
        tbHinhAnh.tbHinhAnhCollection hacollection = tbHinhAnhBus.Get(cri.Criter, Pager.CurrentIndex, Pager.PageSize,
        tbHinhAnh.tbHinhAnhColumns.IDSanPham.ToString(), OrderDirection.ASC.ToString(), out total);
        Pager.PageSize = 5;
        Repeater1.DataSource = hacollection;
        Repeater1.DataBind();
        Pager.ItemCount = total;
        if (total <= Pager.PageSize)
            Pager.Visible = false;
    }

    private void LoadData()
    {
        int id = int.Parse(Request.QueryString["id"].ToString());
        tbSanPham sanpham = tbSanPhamBus.GetByID(id);        
        if (sanpham != null)
        {
            lit_IDSP.Text = id.ToString();
            lit_TenSP.Text = sanpham.TenSanPham;
            lit_dksd.Text = sanpham.DieuKienSuDung;
            lit_giaban.Text = sanpham.GiaBan.ToString();
            lit_gt_thuc.Text = sanpham.GiaTriThuc.ToString();
            lit_mota_sp.Text = sanpham.MoTaSP;
            lit_ngay_bdban.Text = sanpham.NgayBDBan.ToString("dd-MM-yyyy");
            lit_ngay_ketthuc.Text = sanpham.NgayKTBan.ToString("dd-MM-yyyy");
            lit_ngay_update.Text = sanpham.NgayCapNhat.ToString("dd-MM-yyyy");
            lit_sl_ban.Text = sanpham.SoLuongBan.ToString();
            lit_sl_daban.Text = sanpham.SoLuongDaBan.ToString();
            lit_tylehh.Text = sanpham.TiLeHoaHong.ToString();
                tbDiaDiem diadiem = tbDiaDiemBUS.GetByID(sanpham.IDDiaDiem);
                if (diadiem != null)
                {
                    lit_dd.Text = diadiem.DiaDiem.ToString();
                }
                tbDoanhNghiep dn = tbDoanhNghiepBus.GetByID(sanpham.IDDoanhNghiep);
                if (dn != null)
                {
                    lit_dn.Text = dn.TenDoanhNghiep.ToString();
                }
                tbLoaiSP loai = tbLoaiSPBUS.GetByID(sanpham.IDLoaiSP);
                if (loai != null)
                {
                    lit_loai.Text = loai.TenLoaiSP.ToString();
                }
                tbNhanVien nv = tbNhanVienBUS.GetByID(sanpham.IDNhanVien);
                if (nv != null)
                {
                    lit_nv.Text = nv.TenNV.ToString();
                }
                tbTrangThaiSP tt_sp = tbTrangThaiSPBUS.GetByID(sanpham.IDTrangThaiSP);
                if (tt_sp != null)
                {
                    lit_trang_thai.Text = tt_sp.TrangThai.ToString();
                }
            }           
      }

      protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
      {
          if (e.Item.ItemType == ListItemType.Item
                || e.Item.ItemType == ListItemType.AlternatingItem)
          {

              Literal lit_id_hinh = (Literal)e.Item.FindControl("lit_id_hinh");              
              tbHinhAnh ha = (tbHinhAnh)e.Item.DataItem;
              Image img_HinhAnh = (Image)e.Item.FindControl("img_HinhAnh");
              img_HinhAnh.ImageUrl = "~/Admin/Photos/Thumb/" + ha.TenHinh;
              lit_id_hinh.Text = ha.IDHinhAnh.ToString();
          }
      }
      protected void Pager_Command(object sender, CommandEventArgs e)
      {
          Pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
          LoadDataHinh();
      }

    
}
           
         
      
    
   
